package controllers;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;

import model.entities.SalesMatchingDayInfo;
import model.entities.SalesMatchingInvoiceInfo;
import model.entities.SalesMatchingLineidInfo;
import myDaos.MatchingReportDao;


@ManagedBean
@ViewScoped
public class MatchingReportManagedBean {
	private List<SalesMatchingDayInfo> daylist;
	private List<SalesMatchingInvoiceInfo> invlist;
	private List<SalesMatchingLineidInfo> linlist;
	private List<SalesMatchingDayInfo> filtdaylist;
	private List<SalesMatchingInvoiceInfo> filtinvlist;
	private List<SalesMatchingLineidInfo> filtlinlist;
	
	private List<String> dlist;
	private String country;
	private String distributor;
	private Date to_date;
	private Date from_date;
	private boolean isProcedureCalled = false;
	private boolean isMismatchingChecked = false;
	private String  calDay = "CAL_DAY" ;
	private String  invoiceNo = "INVOICE_NO" ;
	private String  lineID = "LINE_ID" ;
	private String groupingLevel = "CAL_DAY" ;
	private List<SelectItem> distList;
	

	@EJB
	private MatchingReportDao mrd;

	@ManagedProperty("#{servicesManagedBean}")
	private ServicesManagedBean servicesManagedBean;
	

	public List<SelectItem> getDistList() {
		return distList;
	}


	public void setDistList(List<SelectItem> distList) {
		this.distList = distList;
	}

	public ServicesManagedBean getServicesManagedBean() {
		return servicesManagedBean;
	}


	public void setServicesManagedBean(ServicesManagedBean servicesManagedBean) {
		this.servicesManagedBean = servicesManagedBean;
	}


	public List<String> getDlist() {
		return dlist;
	}


	public void setDlist(List<String> dlist) {
		this.dlist = dlist;
	}



	public boolean isMismatchingChecked() {
		return isMismatchingChecked;
	}


	public void setMismatchingChecked(boolean isMismatchingChecked) {
		this.isMismatchingChecked = isMismatchingChecked;
	}


	public List<SalesMatchingDayInfo> getFiltdaylist() {
		return filtdaylist;
	}


	public void setFiltdaylist(List<SalesMatchingDayInfo> filtdaylist) {
		this.filtdaylist = filtdaylist;
	}


	public List<SalesMatchingInvoiceInfo> getFiltinvlist() {
		return filtinvlist;
	}


	public void setFiltinvlist(List<SalesMatchingInvoiceInfo> filtinvlist) {
		this.filtinvlist = filtinvlist;
	}


	public List<SalesMatchingLineidInfo> getFiltlinlist() {
		return filtlinlist;
	}


	public void setFiltlinlist(List<SalesMatchingLineidInfo> filtlinlist) {
		this.filtlinlist = filtlinlist;
	}


	public String getCalDay() {
		return calDay;
	}


	public void setCalDay(String calDay) {
		this.calDay = calDay;
	}


	public String getInvoiceNo() {
		return invoiceNo;
	}


	public void setInvoiceNo(String invoiceNo) {
		this.invoiceNo = invoiceNo;
	}


	public String getLineID() {
		return lineID;
	}


	public void setLineID(String lineID) {
		this.lineID = lineID;
	}


	public String getGroupingLevel() {
		return groupingLevel;
	}


	public void setGroupingLevel(String groupingLevel) {
		this.groupingLevel = groupingLevel;
	}


	public List<SalesMatchingDayInfo> getDaylist() {
		return daylist;
	}


	public void setDaylist(List<SalesMatchingDayInfo> daylist) {
		this.daylist = daylist;
	}


	public List<SalesMatchingInvoiceInfo> getInvlist() {
		return invlist;
	}


	public void setInvlist(List<SalesMatchingInvoiceInfo> invlist) {
		this.invlist = invlist;
	}


	public List<SalesMatchingLineidInfo> getLinlist() {
		return linlist;
	}


	public void setLinlist(List<SalesMatchingLineidInfo> linlist) {
		this.linlist = linlist;
	}


	public String getCountry() {
		return country;
	}


	public void setCountry(String country) {
		this.country = country;
	}


	public String getDistributor() {
		return distributor;
	}


	public void setDistributor(String distributor) {
		this.distributor = distributor;
	}


	public Date getTo_date() {
		return to_date;
	}


	public void setTo_date(Date to_date) {
		this.to_date = to_date;
	}


	public Date getFrom_date() {
		return from_date;
	}


	public void setFrom_date(Date from_date) {
		this.from_date = from_date;
	}


	public boolean isProcedureCalled() {
		return isProcedureCalled;
	}


	public void setProcedureCalled(boolean isProcedureCalled) {
		this.isProcedureCalled = isProcedureCalled;
	}


	public void callMatchingProcedure(){
		Boolean procedureReturn;
		procedureReturn=mrd.callMatchingReportProcedure(country, distributor, from_date, to_date);
		if(procedureReturn)
		{
			isProcedureCalled=true;
			getMatchingList();
		}
	}

	public void getMatchingList()
	{
			daylist=mrd.getMatchingReportListDayLevel(null);
			invlist=mrd.getMatchingReportListInvoiceLevel(null);
			linlist=mrd.getMatchingReportListLineidLevel(null);
	}
	
	public void updateMatchingList(){
		filtdaylist=mrd.getMatchingReportListDayLevel("GREEN");
		filtinvlist=mrd.getMatchingReportListInvoiceLevel("GREEN");
		filtlinlist=mrd.getMatchingReportListLineidLevel("GREEN");
	}
	
	
	public void updateDistributorsList()
	{
		//Proposed by Samy
		distList = new ArrayList<SelectItem>();
		for(int i = 0; i < servicesManagedBean.getCountriesDistributorList().size(); i++) 
	    {  
			if(country!=null && !country.trim().equals("") && 
					  servicesManagedBean.getCountriesDistributorList().get(i).getCountry().equals(country))
			{
			distList.add(new SelectItem(
										 servicesManagedBean.getCountriesDistributorList().get(i).getDistCode()
										 ,
										 servicesManagedBean.getCountriesDistributorList().get(i).getDistCode()
					                    )
			             );
			}
	    }
	}
	
	@PostConstruct
	public void init(){
		to_date = new Date();
		GregorianCalendar gc = new GregorianCalendar();
		gc.setTime(to_date);
		gc.add(Calendar.DAY_OF_YEAR, -1);
		from_date = gc.getTime();
	}
}
